//
//  SearchViewController.m
//  pmmSS
//
//  Created by Mac book on 2017/9/13.
//  Copyright © 2017年 WTFSSD. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchBar.h"


#import "Product_verticalCell.h"
#import "Product_ horizontalCell.h"
#import "CustomCollectionViewFlowLayout.h"

#import "FiltrateViewController.h"

@interface SearchViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,WEPopoverControllerDelegate,FiltrateViewControllerDelegate>

/** 选择的结果*/
@property (strong, nonatomic) UILabel *resultLabel;

@property(nonatomic,strong)SearchBar * searchBar;
@property (nonatomic,strong) UIView *buttonView;

@property(nonatomic, assign)BOOL isSquaredLayout;
@property (strong,nonatomic) UICollectionViewFlowLayout *layout;
@property (strong,nonatomic) UICollectionView *collectionView;

//

@property(nonatomic,strong)WEPopoverController * filtratePop;

@end

@implementation SearchViewController

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];


    [self setUpNavBar];
    [self createButtonView];
    [self createCollectionView];
       
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)setUpNavBar{


    self.navigationController.navigationBar.translucent = NO;


    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:RGBA(63, 143, 247, 1) size:CGSizeMake(SCREEN_WIDTH, 64)] forBarMetrics:UIBarMetricsDefault];


    UIButton *leftBtn = [HMButton createButtonWithTitle:nil andTitleColor:nil andBgColor:nil andImage:[UIImage imageNamed:@"icon_arrow_left_white"] andBgImage:nil andSelecter:@selector(clickBarItem:) andTarget:self andTag:101];
    leftBtn.frame = CGRectMake(0, 0, autoScaleW(60), autoScaleW(30));
    [leftBtn setImageEdgeInsets:UIEdgeInsetsMake(autoScaleH(7), 0, autoScaleH(7), autoScaleW(50))];

    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];

    [self.navigationItem setLeftBarButtonItem:leftItem];


    //--------********----------

    _searchBar = [[UINib nibWithNibName:NSStringFromClass([SearchBar class]) bundle:[NSBundle mainBundle]] instantiateWithOwner:nil options:nil].firstObject;
    _searchBar.placeholder = @"工型钢工型钢";
    _searchBar.editEnable = YES;

    self.navigationItem.titleView = _searchBar;

    //--------********----------

   // 空白View


    UIBarButtonItem * changeLayout = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:self.isSquaredLayout?@"icon_layout_2":@"icon_layout_1"].scaleToSize(CGSizeMake(16, 16)) style:UIBarButtonItemStylePlain target:self action:@selector(clickBarItem:)];
    changeLayout.tag = 102;
    changeLayout.tintColor = UIColor.whiteColor;
    self.navigationItem.rightBarButtonItem = changeLayout;



}
-(void)createButtonView{

 
    WeakSelf(weakSelf);

    _buttonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,GET_FRAME_WIDTH(self.view) , autoScaleH(51))];

    _buttonView.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:_buttonView];


    //-------------**************------------

    NSArray *itemsArray = @[@"综合",@"销量",@"价格",@"筛选"];

        for (int i = 0 ; i < 4 ; i++) {

            UIButton *btn = [HMButton createButtonWithTitle:itemsArray[i] andTitleColor:[UIColor blackColor] andBgColor:nil andImage:[UIImage imageNamed:@""] andBgImage:nil andSelecter:@selector(clickBarItem:) andTarget:self andTag:103 + i];
            // btn.selected = YES;
            //[btn setTitleColor:kTextSelectColor forState:UIControlStateSelected];
            //  [btn setBackgroundImage:[UIImage imageWithColor:RGBA(63, 143, 247, 1) size:CGSizeMake(SCREEN_WIDTH, 64)] forState:UIControlStateSelected];


            btn.titleLabel.font = [UIFont fontWithName:default_fontname size:autoScaleW(14)];

            //[btn setTitleEdgeInsets:UIEdgeInsetsMake(autoScaleH(42), 0, 13.2, 0)] ;

            [self.buttonView addSubview:btn];

            [btn mas_makeConstraints:^(MASConstraintMaker *make) {

                make.left.mas_equalTo((weakSelf.buttonView.frame.size.width/4)*i);
                make.width.mas_equalTo(weakSelf.buttonView.frame.size.width/4);
                make.height.mas_equalTo(autoScaleH(50));
                
            }];
            
        }
//-------------**************------------
        for (int i = 0 ; i < 4 ; i++) {

            UIView *lineview =[[UIView alloc]init];
            lineview.backgroundColor = RGB(239, 239, 239);
            [self.buttonView addSubview:lineview];


            if (i < 3) {
                [lineview mas_makeConstraints:^(MASConstraintMaker *make) {

                    make.centerY.mas_equalTo(weakSelf.buttonView);
                    make.left.mas_equalTo((weakSelf.buttonView.frame.size.width/4)*(i + 1));
                    make.width.mas_equalTo(autoScaleW(1));
                    make.height.mas_equalTo(autoScaleH(25));

                }];

            }else{

                [lineview mas_makeConstraints:^(MASConstraintMaker *make) {

                    make.bottom.mas_equalTo(weakSelf.buttonView).offset(-1);
                    make.width.mas_equalTo(weakSelf.buttonView.frame.size.width);
                    make.height.mas_equalTo(autoScaleH(1));

                }];
            }

        };


}
-(void)createCollectionView{


        self.layout = [[UICollectionViewFlowLayout alloc] init];
        self.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        //确定item的大小
       // _layout.itemSize = CGSizeMake((self.view.frame.size.width-1)/2, autoScaleH(250));
        //确定横向间距
        self.layout.minimumLineSpacing = 1;
        //确定纵向间距
        self.layout.minimumInteritemSpacing = 1;
        //确定距离上左下右的距离
        self.layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,autoScaleH(51),GET_FRAME_WIDTH(self.view),GET_FRAME_HEIGHT(self.view)- autoScaleH(51)) collectionViewLayout:self.layout];
        self.collectionView.backgroundColor = RGBA(250, 250, 250, 1);
        //禁止滑动
        self.collectionView.scrollEnabled = YES;
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:self.collectionView];

        [self.collectionView registerClass:[Product_verticalCell class] forCellWithReuseIdentifier:@"Product_verticalCell"];

          [self.collectionView registerClass:[Product__horizontalCell class] forCellWithReuseIdentifier:@"Product_ horizontalCell"];

}

-(void)clickBarItem:(UIButton *)button{

    NSLog(@"button.tag ---- %ld",(long)button.tag);
    switch (button.tag) {
        case 101:
        {
            [self.navigationController popViewControllerAnimated:YES];

        }
            break;
        case 102:
        {
           // 第一种：
            self.collectionView.contentOffset=CGPointMake(0, 0);

            //第二种：
            //[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];

           // 第三种：
           // [self.collectionView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];

            self.isSquaredLayout = !self.isSquaredLayout;
            UIBarButtonItem * changeLayout = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:self.isSquaredLayout?@"icon_layout_2":@"icon_layout_1"].scaleToSize(CGSizeMake(16, 16)) style:UIBarButtonItemStylePlain target:self action:@selector(clickBarItem:)];
            changeLayout.tag = 102;
            changeLayout.tintColor = UIColor.whiteColor;
            self.navigationItem.rightBarButtonItem = changeLayout;

            [self.collectionView reloadData];
        }
            break;
        case 103:
        {

        }
            break;
        case 104:
        {

        }

            break;
        case 105:
        {


        }

            break;
        case 106:
        {
            self.filtratePop = [FiltrateViewController popWithDelegate:self completion:^{
                [[NSNotificationCenter defaultCenter] postNotificationName:ChangeStatusBarStyleNotificationKey object:nil userInfo:@{@"StatusBarStyle":@(UIStatusBarStyleDefault)}];
            }];

        }

            break;
            
        default:
            break;
    }



}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 10;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{


    if (!self.isSquaredLayout) {
        Product_verticalCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Product_verticalCell" forIndexPath:indexPath];

        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    }else{
        Product__horizontalCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Product_ horizontalCell" forIndexPath:indexPath];

        cell.backgroundColor = [UIColor whiteColor];


        return cell;
    }
    return [[UICollectionViewCell alloc] init];


}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"item--------%ld",(long)indexPath.item);
    
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{


    if (!self.isSquaredLayout) {
        return CGSizeMake((self.view.frame.size.width-1)/2, autoScaleH(250));
    }else{
        return CGSizeMake(collectionView.frame.size.width, 122);

    }
    return CGSizeZero;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
